package com.itany.corejava.day12;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class TestColorChooser implements ChangeListener {

    private JFrame frame;
    private JLabel rLabel;
    private JLabel gLabel;
    private JLabel bLabel;
    private JSlider rSlider;
    private JSlider gSlider;
    private JSlider bSlider;
    private JTextField r10Field;
    private JTextField g10Field;
    private JTextField b10Field;
    private JTextField r16Field;
    private JTextField g16Field;
    private JTextField b16Field;
    private JTextArea area;

    public TestColorChooser() {
        frame=new JFrame();
        rLabel=new JLabel("r:");
        gLabel=new JLabel("g:");
        bLabel=new JLabel("b:");
        rSlider=new JSlider(0,255,127);
        gSlider=new JSlider(0,255,127);
        bSlider=new JSlider(0,255,127);
        r10Field=new JTextField(" "+127+" ");
        g10Field=new JTextField(" "+127+" ");
        b10Field=new JTextField(" "+127+" ");
        r16Field=new JTextField(" "+Integer.toHexString(127)+" ");
        g16Field=new JTextField(" "+Integer.toHexString(127)+" ");
        b16Field=new JTextField(" "+Integer.toHexString(127)+" ");
        area=new JTextArea(5,10);
    }

    private void init(){
        JPanel centerPanel=new JPanel();
        JPanel eastPanel=new JPanel();

        centerPanel.add(rLabel);
        centerPanel.add(rSlider);
        centerPanel.add(r10Field);
        centerPanel.add(r16Field);

        centerPanel.add(gLabel);
        centerPanel.add(gSlider);
        centerPanel.add(g10Field);
        centerPanel.add(g16Field);

        centerPanel.add(bLabel);
        centerPanel.add(bSlider);
        centerPanel.add(b10Field);
        centerPanel.add(b16Field);

        eastPanel.add(area);



        frame.add(centerPanel);
        frame.add(eastPanel,BorderLayout.EAST);



    }

    private void setStyle(){

        r10Field.setEditable(false);
        g10Field.setEditable(false);
        b10Field.setEditable(false);
        r16Field.setEditable(false);
        g16Field.setEditable(false);
        b16Field.setEditable(false);
        area.setEditable(false);

        area.setBackground(new Color(127,127,127));
    }

    private void addEventHandler(){
        rSlider.addChangeListener(this);
        gSlider.addChangeListener(this);
        bSlider.addChangeListener(this);
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        frame.setSize(420,160);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestColorChooser().show();
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        int r=rSlider.getValue();
        int g=gSlider.getValue();
        int b=bSlider.getValue();

        r10Field.setText(" "+r+" ");
        g10Field.setText(" "+g+" ");
        b10Field.setText(" "+b+" ");

        r16Field.setText(" "+Integer.toHexString(r)+" ");
        g16Field.setText(" "+Integer.toHexString(g)+" ");
        b16Field.setText(" "+Integer.toHexString(b)+" ");

        area.setBackground(new Color(r,g,b));
    }
}
